﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.AutomaticStoreMotionDAL;
using thinger.AutomaticStoreMotionModels;

namespace thinger.AutomaticStoreMotionPro
{
    public partial class FrmMonitor : Form
    {
        #region 构造函数
        public FrmMonitor()
        {
            InitializeComponent();

            this.Load += FrmMonitor_Load;
        }
        #endregion

        #region 静态字段
        //获取行数
        private int _rowCount = GTSMotionEx.GetInstance().AdvancedParam.nudRowCount;

        //获取列数
        private int _columnCount = GTSMotionEx.GetInstance().AdvancedParam.nudColumnCount;

        //报警清单
        private List<string> _alarmInfoList = new List<string>();

        //当前时间属性
        private string CurrentTime { get { return DateTime.Now.ToString("HH:mm:ss"); } }
        #endregion

        #region 窗口加载
        private void FrmMonitor_Load(object sender, EventArgs e)
        {
            //初始化
            //DataGridView的属性设置
            this.dgvData.AutoGenerateColumns = false;           //自动产生列
            this.dgvSNCode.AutoGenerateColumns = false;

            this.list_Info.Columns[1].Width = this.list_Info.Width - this.list_Info.Columns[0].Width;


            //先添加第一列，列成员类型是文本
            DataGridViewTextBoxColumn header = new DataGridViewTextBoxColumn();
            header.HeaderText = "行/列";
            header.Width = this.dgvData.Width / (_columnCount + 1);
            header.DataPropertyName = "RowIndex";
            header.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dgvData.Columns.Add(header);

            //再添加其他列,列成员类型是图片
            for (int i = 1; i <= _columnCount; i++)
            {
                DataGridViewImageColumn imageHeader = new DataGridViewImageColumn();
                imageHeader.HeaderText = i.ToString();
                imageHeader.Width = (this.dgvData.Width - this.dgvData.Columns[0].Width) / _columnCount - 1;
                imageHeader.DataPropertyName = "Column" + i.ToString().PadLeft(2, '0');
                imageHeader.SortMode = DataGridViewColumnSortMode.NotSortable;
                //imageHeader.AutoSizeMode = i == _columnCount ? DataGridViewAutoSizeColumnMode.Fill : DataGridViewAutoSizeColumnMode.None;
                imageHeader.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
                this.dgvData.Columns.Add(imageHeader);
            }

            this.dgvData.RowTemplate.Height = (this.dgvData.Height - this.dgvData.ColumnHeadersHeight) / _rowCount;

            this.dgvData.CellFormatting += DgvData_CellFormatting;
        }
        #endregion

        #region 窗口操作的委托方法

        /// <summary>
        /// 添加日志信息委托方法
        /// </summary>
        /// <param name="index">等级，0-信息，1-警告，2-报警</param>
        /// <param name="log">报警消息</param>
        public void AddLog(int index, string log)
        {
            if (!this.InvokeRequired)
            {
                ListViewItem item = new ListViewItem("   " + CurrentTime, index);    //列头的初始化
                item.SubItems.Add(log);
                this.list_Info.Items.Add(item);
            }
            else
            {
                this.list_Info.Invoke(new Action(() =>
                {
                    ListViewItem item = new ListViewItem("   " + CurrentTime, index);    //列头的初始化
                    item.SubItems.Add(log);
                    this.list_Info.Items.Add(item);
                }));

            }
        }


        //添加报警条信息委托方法
        public void AddAlarm(string info, bool isAck)
        {
            if (isAck)
            {
                if (!_alarmInfoList.Contains(info))
                {
                    _alarmInfoList.Add(info);
                }
            }
            else
            {
                if (_alarmInfoList.Contains(info))
                {
                    _alarmInfoList.Remove(info);
                }
            }

            RefreshAlarm();
        }
        //更新报警条
        private void RefreshAlarm()
        {
            if (_alarmInfoList.Count == 0)
            {
                this.ledSysState.Value = true;
                this.lbl_Info.Visible = true;
                this.lbl_scrollInfo.Visible = false;
                this.lbl_Info.Text = "系统正常";
            }
            else if (_alarmInfoList.Count == 1)
            {
                this.ledSysState.Value = false;
                this.lbl_Info.Visible = true;
                this.lbl_scrollInfo.Visible = false;
                this.lbl_Info.Text = _alarmInfoList[0];
            }
            else if (_alarmInfoList.Count > 1)
            {
                this.ledSysState.Value = false;
                this.lbl_Info.Visible = false;
                this.lbl_scrollInfo.Visible = true;
                this.lbl_scrollInfo.Text = String.Join("   ", _alarmInfoList.ToArray()).Trim();
            }
        }


        //设置行状态委托方法
        public void SetRowState()
        {
            this.dgvData.DataSource = null;
            this.dgvData.DataSource = GetCurrentRowState();

            //数据监控显示
        }

        private List<RowState> GetCurrentRowState()
        {
            List<ActualStoreData> data = ActualStoreDataService.GetActualStoreData();
            List<RowState> stateList = new List<RowState>();
            if (data.Count >= _rowCount * _columnCount)
            {

                for (int i = 0; i < _rowCount; i++)
                {
                    RowState state = new RowState()
                    {
                        RowIndex = i,
                        Column01 = data[_columnCount * i].StoreState,
                        Column02 = data[_columnCount * i + 1].StoreState,
                        Column03 = data[_columnCount * i + 2].StoreState,
                        Column04 = data[_columnCount * i + 3].StoreState,
                        Column05 = data[_columnCount * i + 4].StoreState,
                        Column06 = data[_columnCount * i + 5].StoreState,
                        Column07 = data[_columnCount * i + 6].StoreState,
                        Column08 = data[_columnCount * i + 7].StoreState,
                        Column09 = data[_columnCount * i + 8].StoreState
                    };
                    stateList.Add(state);
                }
                return stateList;
            }
            return null;
        }


        //批号设定委托方法
        public void SetSNCode(string snCode)
        {
            this.lblSNCode.Text = snCode;
        }


        //条形码委托方法
        public void SetCode(string code)
        {
            this.lblCode.Text = code;
        }


        //重量委托方法
        public void SetWeight(string weigiht)
        {
            this.lblWeight.Text = weigiht;
        }


        //位置托方法
        public void SetPos(string pos)
        {
            this.lblPos.Text = pos;
        }

        #endregion

        #region 物料状态刷新
        private void DgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex >= 1)
                {
                    if (e.Value.ToString() == "1")
                    {
                        e.Value = Properties.Resources.Red;
                    }
                    else
                    {
                        e.Value = Properties.Resources.green;
                    }
                }
            }
        }
        #endregion
    }
}
